package com.xzakota.minesweeper.util;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.File;

/**
 * JSON 文件工具类，一般用来存储设置项
 *
 * @author Xzakota
 */
public class JSONFileUtils {
    private static String jsonPath;
    private static JSONObject jsonObj;

    public static void initJSON(String jsonPath) {
        JSONFileUtils.jsonPath = jsonPath;

        File file = new File(jsonPath);
        if (!file.exists()) {
            jsonObj = new JSONObject();
        } else {
            String source = FileUtils.readFile(jsonPath);
            jsonObj = new JSONObject(source.isEmpty() ? "{}" : source);
        }

    }

    public static Boolean commit() {
        if (jsonObj == null) {
            return false;
        }

        File file = new File(jsonPath);
        if (!FileUtils.createFileIfNotExist(file)) {
            return false;
        }

        FileUtils.writeFileFromString(file, jsonObj.toString(4), false);
        return true;
    }

    public static Boolean getBoolean(String key, Boolean defaultValue) {
        if (has(key)) {
            return jsonObj.getBoolean(key);
        } else {
            return defaultValue;
        }
    }

    public static Double getDouble(String key, Double defaultValue) {
        if (has(key)) {
            return jsonObj.getDouble(key);
        } else {
            return defaultValue;
        }
    }

    public static int getInt(String key, int defaultValue) {
        if (has(key)) {
            return jsonObj.getInt(key);
        } else {
            return defaultValue;
        }
    }

    public static JSONArray getJSONArray(String key) {
        if (has(key)) {
            return jsonObj.getJSONArray(key);
        } else {
            return null;
        }
    }

    public static JSONObject getJSONObject(String key) {
        if (has(key)) {
            return jsonObj.getJSONObject(key);
        } else {
            return null;
        }
    }

    public static String getString(String key) {
        if (has(key)) {
            return jsonObj.getString(key);
        } else {
            return null;
        }
    }

    public static boolean put(String key, Object value) {
        return put(key, value, false);
    }

    public static boolean put(String key, Object value, Boolean isCommit) {
        if (jsonObj == null) {
            return false;
        }

        jsonObj.put(key, value);
        if (isCommit) {
            return commit();
        }

        return true;
    }

    public static boolean putUnlessHas(String key, Object value) {
        return putUnlessHas(key, value, false);
    }

    public static boolean putUnlessHas(String key, Object value, Boolean isCommit) {
        if (jsonObj == null) {
            return false;
        }

        if (!has(key)) {
            return put(key, value, isCommit);
        } else {
            return false;
        }
    }

    public static Object remove(String key) {
        return jsonObj.remove(key);
    }

    public static boolean has(String key) {
        if (jsonObj == null) {
            return false;
        }
        return jsonObj.has(key);
    }
}
